<?php
(!defined('IN_SYSTEM') || !defined('ADMIN_PRE')) && exit('Access Denied!');
$f = isset($_REQUEST['f'])?$_REQUEST['f']:'index';
$m = new OpeModule;
$m->$f();
/*
 CREATE TABLE IF NOT EXISTS `wm_opelog` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `uid` int(10) unsigned NOT NULL DEFAULT '0',
  `code` char(6) NOT NULL DEFAULT '',
  `bdate` int(11) NOT NULL DEFAULT '0',
  `btotal` decimal(10,3) NOT NULL DEFAULT '0.000',
  `bdapan` decimal(10,3) NOT NULL DEFAULT '0.000',
  `bprice` decimal(10,3) NOT NULL DEFAULT '0.000',
  `bamount` int(10) unsigned NOT NULL DEFAULT '0',
  `sdate` int(11) NOT NULL DEFAULT '0',
  `sdapan` decimal(10,3) NOT NULL DEFAULT '0.000',
  `sprice` decimal(10,3) NOT NULL DEFAULT '0.000',
  `samount` int(10) unsigned NOT NULL DEFAULT '0',
  `stotal` decimal(10,3) NOT NULL DEFAULT '0.000',
  `lprice` decimal(10,3) NOT NULL DEFAULT '0.000',
  `bshouxu` decimal(10,3) NOT NULL DEFAULT '0.000',
  `bshui` decimal(10,3) NOT NULL DEFAULT '0.000',
  `bguohu` decimal(10,3) NOT NULL DEFAULT '0.000',
  `sshouxu` decimal(10,3) NOT NULL DEFAULT '0.000',
  `sshui` decimal(10,3) NOT NULL DEFAULT '0.000',
  `sguohu` decimal(10,3) NOT NULL DEFAULT '0.000',
  `body` text,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;
 */
class OpeModule
{
	function index() {//列表
		global $_G;
		$wheresql = '';
		$page = max(1, getGP('page','G','int'));
		$pagesize = 27;
		$offset = ($page - 1) * $pagesize;
		$url = 'admin.php?m=opelog';
	
		if (getGP('opdate','G')) {
			$opdate=getGP('opdate','G');
			$optime=gtime($opdate);
			$wheresql.=" AND l.bdate>=".$optime;
			$m=gdate('m',$optime);
			$y=gdate('Y',$optime);
			if($m<12){
				$m+=1;
			}else{
				$m=1;
				$y+=1;
			}
			$sqtime=gtime($y.'-'.$m);
			if($optime>0)$wheresql.=" AND l.sdate<$sqtime AND l.bdate<$sqtime";
			$url .= '&opdate='.$opdate;		
		}else{
			$opdate=gdate('Y-m',TIMESTAMP);
			$wheresql.=" AND l.bdate>=".gtime($opdate);
		}
		
		//总盈利:亏损次数,盈利次数，盈亏比，总亏损，总盈利，风险回报率
		//$stat=$_G['db']->fetch_one_array("SELECT count(id) as num,sum(stotal) as total FROM".table('opelog')."WHERE 1 $wheresql");
		//$num = $_G['db']->result("SELECT COUNT(*) AS num FROM".table('opelog')."l WHERE 1 $wheresql");
		//期初
		$start = $_G['db']->fetch_one_array("SELECT l.btotal,bdapan,bdate FROM".table('opelog')."l  WHERE l.uid='$_G[uid]' AND l.btotal>0 $wheresql ORDER BY l.id ASC LIMIT 1");
		$minstart= $_G['db']->result("SELECT bdate FROM".table('opelog')."l  WHERE l.uid='$_G[uid]' AND l.bdate>0 ORDER BY l.bdate ASC LIMIT 1");
		if($minstart>0){
			while($minstart<TIMESTAMP){
				$searchs[gdate('Y-m',$minstart)]=gdate('Y年m月',$minstart);
				$while_m=gdate('m',$minstart);
				$while_y=gdate('Y',$minstart);
				if($while_m<12){
					$while_m+=1;
				}else{
					$while_m=1;
					$while_y+=1;
				}
				$minstart=gtime($while_y.'-'.$while_m);
			}
		}
		$loss=$p=$num=$fee=$lossnum=$lossmaxnu=$stopnum=$lossmaxnum=$maxid=$cnum=$dapannum=$pmaxnu=$pmaxnum=$pnum=$lossmax=$pmax=0;
		$totalnum=$_G['db']->result("SELECT count(*) FROM".table('opelog')."l  WHERE l.uid='$_G[uid]' $wheresql");
		$sql = "SELECT l.* FROM".table('opelog')."l  WHERE l.uid='$_G[uid]' $wheresql";
		$query=$_G['db']->query($sql);
		while($rs=$_G['db']->fetch_array($query)){
			$num++;
			if($rs['stotal']>0)$cnum++;//总次数
			if($rs['stotal']>0 && $rs['id']>$maxid){//期末总值
				$stotal=$rs['stotal'];
				$sdapan=$rs['sdapan'];
				$maxid=$rs['id'];
			}
			//费用
			$_fee=$rs['sprice']*$rs['samount']-$rs['bprice']*$rs['bamount']-$rs['stotal']+$rs['btotal'];
			if($rs['stotal']>0  && $rs['stotal']>$rs['btotal']+$_fee ){//盈利次数和连续盈利次数
				$p+=$rs['stotal']-$rs['btotal'];
				if($rs['stotal']-$rs['btotal']>$pmax)$pmax=$rs['stotal']-$rs['btotal'];
				$pnum++;
				$pmaxnu++;
				//达到4R的盈利次数
				$R=($rs['stotal']-$rs['btotal'])/(($rs['bprice']-$rs['lprice'])*$rs['bamount']+$_fee);
				if($R>9.99) $_10R++;
				if($R>3.99) $_4R++;
				if($R>2.99) $_3R++;
				if($R>1.99) $_2R++;
				if($R>0.99) $_1R++;
			}else{
				$pmaxnu=0;
			}
			if($pmaxnu>$pmaxnum){//最大连续盈利次数
				$pmaxnum=$pmaxnu;
			}
			if($rs['stotal']>$maxtotal){//取得最大的总资金额
				$maxtotal=$rs['stotal'];
			}
			if($rs['sdapan']>0 && $rs['sdapan']>$rs['bdapan']){//大盘涨跌
				$dapannum++;
			}
			if($rs['stotal']>0 && $rs['stotal']<$rs['btotal']){//亏损
				$loss+=$rs['stotal']-$rs['btotal'];//总亏损
				if($rs['btotal']-$rs['stotal']>$lossmax)$lossmax=$rs['btotal']-$rs['stotal'];
				if($rs['sprice']<$rs['bprice']){//亏损,不含费用
					$lossp+=$rs['sprice']*$rs['samount']-$rs['bprice']*$rs['bamount'];
				}
				$lossnum++;
				$lossmaxnu++;
				if(($rs['btotal']-$rs['stotal'])/$rs['btotal']>0.0199)$stopnum++;
			}else{
				$lossmaxnu=0;
			}
			if($lossmaxnu>$lossmaxnum){//最大连续亏损次数
				$lossmaxnum=$lossmaxnu;
			}
			if($rs['stotal']>0){
				$fee+=$_fee;
				$rate+=$rs['bprice']*$rs['bamount'];
				$allrate+=$rs['btotal'];
			}
			$results[]=$rs;
		}
		if(empty($start)){
			$start = $_G['db']->fetch_one_array("SELECT l.btotal,bdapan,bdate,stotal FROM".table('opelog')."l  WHERE l.uid='$_G[uid]' AND sdate >1 ORDER BY l.id DESC LIMIT 1");
			$start['bdate']=TIMESTAMP;
			$start['btotal']=$start['stotal'];
			$sdapan=$start['bdapan'];
			$stotal=$start['stotal'];
		}
		if(empty($stotal))$stotal=$start['btotal'];
		if(empty($sdapan))$sdapan=$start['bdapan'];
		if(empty($maxtotal))$maxtotal=$start['btotal'];
		
		$results = $_G['db']->fetch_all("SELECT l.* FROM".table('opelog')."l  WHERE l.uid='$_G[uid]' $wheresql ORDER BY l.id DESC LIMIT $offset, $pagesize");
		include admintemplate('opelog');
	}
	function stat(){
		global $_G;
		
		include admintemplate('opelog');
	}
	function add(){
		global $_G;
		$stat=$_G['db']->fetch_one_array("SELECT stotal,sdapan FROM".table('opelog')."WHERE stotal>0 ORDER BY id DESC limit 1");
		include admintemplate('opelog');
	}
	function save(){
		global $_G;
		$savetype = getGP('savetype','P');
		$id = getGP('id','P','int');
		$data = array(
			'id'=>$id?$id:null,
			'uid'=>$_G['uid'],
		    'code' => getGP('code','P'),
			 'bdate' => gtime(getGP('bdate','P')),
			 'btotal' => getGP('btotal','P'),
			 'bdapan' => getGP('bdapan','P'),
			 'bprice' => getGP('bprice','P'),
			 'bamount' => getGP('bamount','P'),
			 'lprice' => getGP('lprice','P'),
			 'sdate' => gtime(getGP('sdate','P')),
			 'stotal' => getGP('stotal','P'),
			 'sdapan' => getGP('sdapan','P'),
			 'sprice' => getGP('sprice','P'),
			 'samount' => getGP('samount','P'),
			 'bshouxu' => getGP('bshouxu','P'),
			 'bshui' => getGP('bshui','P'),
			 'bguohu' => getGP('bguohu','P'),
			 'sshouxu' => getGP('sshouxu','P'),
			 'sshui' => getGP('sshui','P'),
			 'sguohu' => getGP('sguohu','P'),
			 'body'	=> getGP('body','P'),
		);
		$_G['db']->insert('opelog',$data,true);
		msg('操作成功',url("admin.php?m=opelog"));
	}
	function edit(){
		global $_G;
		$id = getGP('id','G','int');
		if ($result = $_G['db']->fetch_one_array("SELECT * FROM".table('opelog')."WHERE id = '$id'")) {
			include admintemplate('opelog');
		} else {
			msg('');
		}
	}
}
?>